/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_VECTOR_HPP
#define XERN_VECTOR_HPP

#include <boost/io/ios_state.hpp>
#include <iostream>
#include <cassert>
#include "MathCommon.hpp"

// Bullet include
#ifdef XERN_USE_BULLET
#include <LinearMath/btVector3.h>
#endif

namespace Xern
{

#ifndef XERN_SIMD_MATH
struct QuadFloat
{
#ifndef SWIG
    union
    {
        struct
        {
            float x, y, z, w;
        };

        struct
        {
            float i, j, k, r;
        };

        float dataArray[4];
    };
#else
    float x, y, z, w;
#endif
};
#endif

class Vector2
{
public:
    Vector2() {}
    Vector2(float v)
            : x(v), y(v) {}
    Vector2(float cx, float cy)
            : x(cx), y(cy) {}
    Vector2(const Vector2 &v)
            : x(v.x), y(v.y) {}

    float Length() const
    {
        return std::sqrt(x*x + y*y);
    }

    float Length2() const
    {
        return x*x + y*y;
    }

    float Dot(const Vector2 &v) const
    {
        return x*v.x + y*v.y;
    }

    void Normalize()
    {
        float l = Length();
        if (l != 0)
        {
            x /= l;
            y /= l;
        }
    }

    void Set(float vx, float vy)
    {
        x = vx;
        y = vy;
    }

    Vector2 &operator+=(const Vector2 &v)
    {
        x +=v.x;
        y += v.y;
        return *this;
    }

    Vector2 &operator-=(const Vector2 &v)
    {
        x -= v.x;
        y -= v.y;
        return *this;
    }

    Vector2 &operator*=(float s)
    {
        x *= s;
        y *= s;
        return *this;
    }

    Vector2 &operator/=(float s)
    {
        x /= s;
        y /= s;
        return *this;
    }

    friend Vector2 operator-(const Vector2 &v)
    {
        return Vector2(-v.x, -v.y);
    }

    friend Vector2 operator+(const Vector2 &v1, const Vector2 &v2)
    {
        return Vector2(v1.x + v2.x, v1.y + v2.y);
    }

    friend Vector2 operator-(const Vector2 &v1, const Vector2 &v2)
    {
        return Vector2(v1.x - v2.x, v1.y - v2.y);
    }

    friend Vector2 operator*(const Vector2 &v, float s)
    {
        return Vector2(v.x*s, v.y*s);
    }

    friend Vector2 operator*(float s, const Vector2 &v)
    {
        return Vector2(v.x*s, v.y*s);
    }

    friend Vector2 operator*(const Vector2 &v1, const Vector2 &v2)
    {
        return Vector2(v1.x*v2.x, v1.y*v2.y);
    }

    friend Vector2 operator/(const Vector2 &v, float s)
    {
        return Vector2(v.x/s, v.y/s);
    }

    friend Vector2 operator/(const Vector2 &v1, const Vector2 &v2)
    {
        return Vector2(v1.x/v2.x, v1.y/v2.y);
    }

    friend bool operator ==(const Vector2 &v1, const Vector2 &v2)
    {
        if (v1.x == v2.x && v1.y == v2.y)
            return true;
        return false;
    }

    friend bool operator !=(const Vector2 &v1, const Vector2 &v2)
    {
        if (v1.x != v2.x || v1.y != v2.y)
            return true;
        return false;
    }

    float &operator[](int index)
    {
        // Sanity check
        assert(index == 0 || index == 1);

        switch (index)
        {
        case 0:
            return x;
            break;
        case 1:
            return y;
            break;
        default: // Never gets here
            return x;
        }
    }

    float x, y;
};

class XERN_ALIGNED(16) Vector3: public QuadFloat
{
public:
    Vector3() {}
    explicit Vector3(float v)
    {
        x=v;
        y=v;
        z=v;
    }

    Vector3(float cx, float cy, float cz)
    {
        x = cx;
        y = cy;
        z = cz;
    }

    Vector3(const Vector3 &v)
    {
        x = v.x;
        y = v.y;
        z = v.z;
    }

#ifdef XERN_USE_BULLET
    Vector3(const btVector3 &b)
    {
        x = b.x();
        y = b.y();
        z = b.z();
    }
#endif

    float Length() const
    {
        return squareroot(x*x + y*y + z*z);
    }

    float Length2() const
    {
        return x*x + y*y + z*z;
    }

    float Dot(const Vector3 &v) const
    {
        return x*v.x + y*v.y + z*v.z;
    }

    Vector3 Cross(const Vector3 &v) const
    {
        return Vector3(y*v.z - z*v.y,
                       z*v.x - x*v.z,
                       x*v.y - y*v.x);
    }

    Vector3 Normalize() const
    {
        float l2 = x*x+y*y+z*z;
        if (CompareFloat(l2, 0.0f) != 0)
            l2 = invsquareroot(l2);
        return (*this)*l2;
    }

    void Set(float vx, float vy, float vz)
    {
        x = vx;
        y = vy;
        z = vz;
    }

    Vector3 &operator+=(const Vector3 &v)
    {
        x +=v.x;
        y += v.y;
        z += v.z;
        return *this;
    }

    Vector3 &operator-=(const Vector3 &v)
    {
        x -= v.x;
        y -= v.y;
        z -= v.z;
        return *this;
    }

    Vector3 &operator*=(float s)
    {
        x *= s;
        y *= s;
        z *= s;
        return *this;
    }

    Vector3 &operator/=(float s)
    {
        s = 1.0f / s;
        x *= s;
        y *= s;
        z *= s;
        return *this;
    }

    friend bool operator ==(const Vector3 &v1, const Vector3 &v2)
    {
        if (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z)
            return true;
        return false;
    }

    friend bool operator !=(const Vector3 &v1, const Vector3 &v2)
    {
        if (v1.x != v2.x || v1.y != v2.y || v1.z != v2.z)
            return true;
        return false;
    }

    friend Vector3 operator-(const Vector3 &v)
    {
        return Vector3(-v.x, -v.y, -v.z);
    }

    friend Vector3 operator+(const Vector3 &v1,
                             const Vector3 &v2)
    {
        return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
    }

    friend Vector3 operator-(const Vector3 &v1,
                             const Vector3 &v2)
    {
        return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
    }

    friend Vector3 operator*(const Vector3 &v, float s)
    {
        return Vector3(v.x*s, v.y*s, v.z*s);
    }

    friend Vector3 operator*(float s, const Vector3 &v)
    {
        return Vector3(v.x*s, v.y*s, v.z*s);
    }

    friend Vector3 operator*(const Vector3 &v1,
                             const Vector3 &v2)
    {
        return Vector3(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
    }

    friend Vector3 operator/(const Vector3 &v, float s)
    {
        return Vector3(v.x/s, v.y/s, v.z/s);
    }

    friend Vector3 operator/(const Vector3 &v1,
                             const Vector3 &v2)
    {
        return Vector3(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
    }

    float &operator[](int index)
    {
        // Sanity check
        assert(index >= 0 && index < 3);
        switch (index)
        {
        case 0:
            return x;
            break;
        case 1:
            return y;
            break;
        case 2:
            return z;
            break;
        default: // Never gets here
            return x;
        }
    }

    Vector3 &operator=(const Vector3 &v)
    {
        x = v.x;
        y = v.y;
        z = v.z;
        return *this;
    }

#ifdef XERN_USE_BULLET
    Vector3 &operator=(const btVector3 &b)
    {
        x = b.x();
        y = b.y();
        z = b.z();
        return *this;
    }

    operator btVector3() const
    {
        return btVector3(x, y, z);
    }
#endif

};

class CompactVector3
{
public:
    CompactVector3() {}
    explicit CompactVector3(float v)
            : x(v), y(v), z(v) {}

    CompactVector3(float cx, float cy, float cz)
            : x(cx), y(cy), z(cz) {}

    CompactVector3(const Vector3 &v)
            : x(v.x), y(v.y), z(v.z) {}

    CompactVector3(const CompactVector3 &v)
            : x(v.x), y(v.y), z(v.z) {}

    CompactVector3 &operator=(const CompactVector3 &other)
    {
        x = other.x;
        y = other.y;
        z = other.z;
        return *this;
    }

    CompactVector3 &operator=(const Vector3 &other)
    {
        x = other.x;
        y = other.y;
        z = other.z;
        return *this;
    }

    float x, y, z;
};

class SVector4: public Vector3
{
public:
    SVector4() {}
    SVector4(float v)
            : Vector3(v)
    {
        w = v;
    }

    SVector4(float cx, float cy, float cz, float cw=1.0f)
            : Vector3(cx, cy, cz)
    {
        w = cw;
    }

    SVector4(const Vector3 &v)
            : Vector3(v)
    {
        w = 1.0f;
    }

    SVector4(const SVector4 &v)
            : Vector3(v)
    {
        w = v.w;
    }

    SVector4 &operator=(const Vector3 &v)
    {
        x = v.x;
        y = v.y;
        z = v.z;
        return *this;
    }

    SVector4 &operator=(const SVector4 &v)
    {
        x = v.x;
        y = v.y;
        z = v.z;
        w = v.w;
        return *this;
    }

    float Dot(const SVector4 &v) const
    {
        return x*v.x + y*v.y + z*v.z + w*v.w;
    }
};

inline std::ostream &operator<<(std::ostream &os, const Vector2 &v)
{
	const std::ostream::sentry ok(os);

	if(ok)
	{
		os << v.x << " " << v.y;
	}

    return os;
}

inline std::istream &operator>>(std::istream &is, Vector2 &v)
{
	const std::istream::sentry ok(is, false);
	boost::io::ios_flags_saver s(is);

	if(ok)
	{
		is >> std::skipws >> v.x >> v.y;
	}

    return is;
}

inline std::ostream &operator<<(std::ostream &os, const Vector3 &v)
{
	const std::ostream::sentry ok(os);

	if(ok)
	{
		os << v.x << " " << v.y << " " << v.z;
	}

    return os;
}

inline std::istream &operator>>(std::istream &is, Vector3 &v)
{
	const std::istream::sentry ok(is, false);
	boost::io::ios_flags_saver s(is);


	if(ok)
	{
		is >> std::skipws >> v.x >> v.y >> v.z;
	}

    return is;
}

inline std::ostream &operator<<(std::ostream &os, const SVector4 &v)
{
	const std::ostream::sentry ok(os);

	if(ok)
	{
		os << v.x << " " << v.y << " " << v.z << " " << v.w;
	}

    return os;
}

inline std::istream &operator>>(std::istream &is, SVector4 &v)
{
	const std::istream::sentry ok(is, false);
	boost::io::ios_flags_saver s(is);

	if(ok)
	{
		is >> std::skipws >> v.x >> v.y >> v.z >> v.w;
	}

    return is;
}

template<typename A, typename B>
int CompareVector3(const A &a, const B &b)
{
	int ret = CompareFloat(a.x, b.x);
	if(ret != 0)
		return ret;
	ret = CompareFloat(a.y, b.y);
	if(ret != 0)
		return ret;
	return CompareFloat(a.z, b.z);
}

} // namespace Xern

#endif //XERN_VECTOR_HPP

